<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: Think
  Date: 2024/7/23
  Time: 下午5:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    //获取session创建时间.
    Date createTime = new Date(session.getCreationTime());
    //获取最后访问页面的事件.
    Date lastTime = new Date(session.getLastAccessedTime());

    String title = "再次访问实例页面";
    Integer visitCount = 0;
    String visitCountKey = "visitCount";
    String userIdKey = "userId";
    String userId = "ABCD";

    //检查网页是否有新的访问用户
    if (session.isNew()){
        title = "初次访问实例";
        session.setAttribute(userIdKey,userId);
        session.setAttribute(visitCountKey,visitCount);
    }else {
        visitCount = (Integer) session.getAttribute(visitCountKey);
        visitCount+=1;
        userId = (String) session.getAttribute(userIdKey);
        session.setAttribute(visitCountKey,visitCount);
    }
%>
<html>
<head>
    <title>JSP Session的应用</title>
</head>
<body>
<h1>Session跟踪</h1>

<table border = "1"align="center">
    <tr bgcolor="#faebd7"><th>Session信息</th><th>值</th></tr>
    <tr>
        <td>id</td>
        <td><% out.print(session.getId());%></td>
    </tr>
    <tr>
        <td>创建时间</td>
        <td><% out.print(createTime);%></td>
    </tr>
    <tr>
        <td>最后访问时间</td>
        <td><% out.print(lastTime);%></td>
    </tr>
    <tr>
        <td>用户ID</td>
        <td><% out.print(userId);%></td>
    </tr>
    <tr>
        <td>访问次数</td>
        <td><% out.print(visitCount);%></td>
    </tr>
</table>
</body>
</html>
